package com.edu.controller;

import com.edu.bean.Goods;
import com.edu.bean.Page;
import com.edu.service.GoodsService;
import com.edu.service.impl.GoodsServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Author:xgdxqr
 * @Date:2025/9/19-14:45-25
 **/

@WebServlet("/getGoods.do")
public class GetGoodsServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        GoodsService goodsService = new GoodsServiceImpl();
        Page page = null;
        int pageSize;
        int pageNum;
        int totalRow = goodsService. getAllGoodsRows();
        try {
            pageSize = Integer.parseInt(req.getParameter("pageSize"));
        } catch (Exception e) {
            pageSize = 5;
        }
        try {
            pageNum = Integer.parseInt(req.getParameter("pageNum"));
        } catch (Exception e) {
            pageNum = 1;
        }
        List<Goods> listGoods = goodsService.getGoods( pageSize, pageNum);
        page = new Page(listGoods,pageNum,pageSize,totalRow);
        req.setAttribute("page", page);
        req.getRequestDispatcher("success.jsp").forward(req, resp);
    }
}
